home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: presby.edu!jtbell
- From: jtbell@presby.edu (Jon Bell)
- Subject: Re: What are the differences between structures and classes in C++ ?
- Message-ID: <DpG53J.Hsz@presby.edu>
- Date: Sat, 6 Apr 1996 15:16:31 GMT
- References: <4k5m65$av@hpscit.sc.hp.com>
- Organization: Presbyterian College, Clinton, South Carolina USA
-
- Raghuveera Ravinutala <raghur> wrote:
- > Please mail me the differences between structures and classes in C++.
- >Raghu.
-
- Sorry, I can't mail this to you because you didn't provide a complete
- e-mail address. But I'll post it anyway...
-
- As far as I can tell, the only *formal* difference between a class and a
- struct is that by default, class members are private whereas struct
- members are public. Classes and structs can each contain both member data
- and member functions. Therefore, the following declarations are all
- equivalent:
-
- // version 1
-
- class Foo
- {
- double Foodata;
- public:
- double GetFoodata (void) const;
- void SetFoodata (double);
- };
-
- // version 2
-
- class Foo
- {
- private:
- double Foodata;
- public:
- double GetFoodata (void) const;
- void SetFoodata (double);
- };
-
- // version 3
-
- struct Foo
- {
- double GetFoodata (void) const;
- void SetFoodata (double);
- private:
- double Foodata;
- };
-
- // version 4
-
- struct Foo
- {
- public:
- double GetFoodata (void) const;
- void SetFoodata (double);
- private:
- double Foodata;
- };
-
- Everybody uses classes for a situation like this one, rather than
- structs, but I haven't found anything (yet) in Stroustrup's "The C++
- Programming Language" that would forbid using a struct instead.
-
- --
- Jon Bell <jtbell@presby.edu> Presbyterian College
- Dept. of Physics and Computer Science Clinton, South Carolina USA
-